let values = localStorage.getItem("jwt");
let old_brief;  // 用来提前获取简介的内容的
let textarea = document.querySelector(".edit_box textarea");
let submit = document.querySelector(".send input");
(function () {
    fetch('/user_message/userBrief', {
        method: 'GET',
        headers: {
            'authorization': values
        }
    }).then(response => {
        if (!response.ok) {
            throw new Error('网络请求错误：' + response.status + ' ' + response.statusText);
        }
        return response.json();
    }).then(data => {
        data[0]['profile'] ? textarea.value = data[0]['profile'] : '';  // 如果有简介的话就填充无的话就不填充
        old_brief = textarea.value;
    }).catch(error => {
        console.log(error);
    })
})();




submit.addEventListener('click', (e) => {
    e.preventDefault(); // 阻止表单默认提交行为
    // 验证用户名是否改变且不为空，长度也要小于15个字符
    if (textarea.value === old_brief) {
        window.location.href = "/webpage_copy/my/my-editdata.html";
    } else if (textarea.value.length >= 100) {
        alert('有太多话建议去发帖哦!');
    } else {
        let data = {
            brief: textarea.value
        };
        fetch('/user_message/updateBrief', {
            method: 'POST',
            headers: {
                'authorization': values,
                'content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        }).then(response => {
            if (!response.ok) {
                throw new Error('网络请求错误: ' + response.status + ' ' + response.statusText);
            }
            return response.json();
        }).then(() => {
            // 更新成功后的操作
            window.location.href = "/webpage_copy/my/my-editdata.html";
        })
            .catch(error => {
                console.error(error);
            });

    }
})





